package main;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.URI;
import java.util.HashMap;
import java.util.UUID;

import jsonobj.ClusterLoginInfo;
import jsonobj.Response;

import data.WebSocket;

import threads.MonitorThread;
import threads.ReceiveThread;
import utils.Msg;
import utils.Utils;

public class ClusterClient {
	String hostname;
	Thread rthread;
	Thread monthread;

	public ClusterClient() {
		try {
			//WebSocket ws = new WebSocket(new URI("ws://localhost:8080/wstest/"));
			Utils.ws = new WebSocket(new URI("ws://sire.dyndns.org/wstest/"));
			Utils.ws.connect();
			Utils.out("+ connected to server");
			Utils.ws.send(new ClusterLoginInfo(InetAddress.getLocalHost().getHostName(),Utils.username,Utils.passwd));
			Utils.out("+ sent login data");
			String res = Utils.ws.recv();
			Response resp = Response.fromJSon(res);
			if (resp.success) {
				Utils.out("+ login success");
				rthread = new ReceiveThread();
				monthread = new MonitorThread();
				rthread.start();
				monthread.start();
				Utils.out("+ waiting for jobs");
			} else {
				Utils.out("- login failed");
				//Msg.printLoginErr(resp.err);
				System.exit(0);
			}

		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
	}

	public static void main(String[] args) {
		if (args.length < 2) {
			/*System.out
					.println("usage: java ClusterClient <username> <password>");*/
			Utils.username = "test5";
			Utils.passwd = "test55";
			// System.exit(0);
		}
		else
		{
			Utils.username = args[0];
			Utils.passwd = args[1];
		}
		
		
		new ClusterClient();
	}
}
